import '../libs/wordExport/jquery.wordexport'

export default function wordExport() {
  let cloneDemo = $('#word-content').clone()
  let fileName = cloneDemo.find('h3').text().trim()

  //移除多余元素
  cloneDemo.find('.synopsis').remove()
  cloneDemo.find('.title_p0').remove()
  // title_p2替换
  cloneDemo.find('h3').after('<div class="title_p2"><span>部门：<span class="underline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;</span></span>&nbsp;&nbsp;&nbsp;<span>姓名 ：<span class="underline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;</span></span></div>')

  //选择框 ico替换
  cloneDemo.find(".options dd span.f1").html('□')
  // 判断题选项替换
  cloneDemo.find('.select-i-true').append("<span>√</span>").find("i").remove()
  cloneDemo.find('.select-i-false').append("<span>×</span>").find("i").remove()

  // 问题括号替换，题型去除 strong替换成p 
  let strongDom = cloneDemo.find('dt strong')
  strongDom.replaceWith((index: number) => {
    let nowStrongDom = $(strongDom[index])
    let question = nowStrongDom.find('span.t2').html().replace(/&nbsp;\(&nbsp;&nbsp;&nbsp;\)&nbsp;/g, '(&nbsp;)').replace(/【.+】/, '')
    let i = nowStrongDom.find("span.t1").text()
    return '<p class="question_title">' + i + question + '</p>'
  })

  // 填空题input替换
  let blankInput = cloneDemo.find("dd.blank-dd input")
  blankInput.replaceWith((index: number) => {
    return '<p class="opt-l">填空' + $(blankInput[index]).attr('data-index') + '&nbsp;&nbsp;</p>'
  })

  // 问答题textarea替换
  let textOption = cloneDemo.find("dd.text-dd textarea")
  textOption.replaceWith(() => {
    return '<div class="text-textarea"></br></br></br></br></div>'
  })

  let ddDom = cloneDemo.find("dd")
  ddDom.replaceWith((index: number) => {
    return '<p class="opt-l">' + ddDom[index].innerHTML + '</p>'
  })


  //样式  不能多级样式 eg: .a.b{... }
  let style = `
    .a{}
    .title_p2 {
      font-size:10.5pt;
      color:#333;
      text-align:center;
    }
    h3 {
      font-size: 14pt;
      font-weight:bold;
      color:#333333;
      text-align:center;
      margin:0 40px 26px;
    }
    h4 {
      font-size: 12pt;
      font-weight: bold;
      color: #666;
    }
    .opt-l{
      margin-top: 10px;
      margin-left: 20px;
      margin-bottom: 10px;
      padding: 0;
      font-size: 12pt;
    }
    .question_title{
      font-weight: bold;
      margin-top: 30px;
    }
    .question-type-box{
      margin:0 0 40px;
    }
    .text-textarea {
      border:1px solid #ddd;
      margin-left: 20px;
    }
    .underline {
      text-decoration: underline;
    }
    .f1 {
      margin-left: 20px;
    }
    .a{}`

  //生成word
  cloneDemo.wordExport({
    style: style,
    fileName: fileName
  })

}